# -*- coding: utf-8 -*-
from src.com.ydzy.util.base_util import BaserUtil
from src.com.ydzy.util.http_util import HttpClient
from src.com.ydzy.util.sql_util import MySQLUtils

log_types=\
{
    "01":"认证日志",
    "02":"业务安全策略控制日志",
    "05":"环境感知日志",
    "06":"应用系统操作日志",
    "07":"应用服务日志",
    "08":"数据服务日志",
    "09":"流量（检控）日志",
    "0301": "权限管理-令牌处理日志",
    "0302": "权限管理-令牌处理日志",
    "0303": "权限管理-鉴权服务日志",
    "0304":"权限管理-风险日志",
    "0401": "业务审批-审批流程管理日志",
    "0402": "业务审批-审批请求日志",
    "0403": "业务审批-审批操作日志",
    "0404":"业务审批-风险日志"
}

http_client = HttpClient()
base_client=BaserUtil()
req_url='http://192.168.253.158:20028/auditcenter/log_strategies'
# req_url='http://192.168.253.158:20028/auditcenter/demo/v1'
# req_url='http://192.168.9.61:8580/auditcenter/system_logs?pageSize=10&pageNum=1&searchCount=true&sortFields=-logTime&params=%7B%7D'
base_param={
    "logType": "",
    "sysId": "",
    "name": "",
    "type": "01", #策略类型strategy_type（01基础，02特定场合）
    "status": "1",
    "auditStatus": "0",
    "desc": "基础测试策略",
    "innerSwitch": True,
}
field_base_param={
            "status": 1,
            "fieldName": "logTime",
            "fieldZh": "日志时间",
            "fieldDesc": "日志时间",
            "fieldType": "01",
            "fieldFormat": "",
            "usageOpt": "01,02",
            "validateType": "02",
            "isMust": "1",
            "isInternal": "1",
            "dataRule": "",
            "expression": "",
            "blockType": "1",
            "ruleReverse": "0",
            "mustAdvise": "日志业务时间完整性检查项",
            "ruleAdvise": "日志业务时间规则性检查项",
            "mustScore": 50,
            "ruleScore": 50,
            "queryOpt": "01,02",
            "pageOptJson": {
                "field": "logTime",
                "type": "input",
                "label": "logTime",
                "optionName": "",
                "optionKey": "",
                "options": []
            },
            "showOpt": "01,02,03",
            "relType": "02",
            "sortNum":1,
        }
field_desc_dict={
    "operateType":"日志操作类型",
    "policeNo": "警号",  # 暂时没有
    "object": "主体",
    "subject": "客体",
    "businessIp": "业务发生IP",
    "terminalIp":"终端Ip",
    "protocolType":"网络协议类型", # 暂时没有
    "srcIp":"网络访问源IP", # 暂时没有
    "destIp":"网络访问目标IP", # 暂时没有
    "srcPort":"网络访问源端口",# 暂时没有
    "destPort":"网络访问目标端口",# 暂时没有
    "userToken":"用户令牌",
    "appToken":"应用令牌",
    "dataLevel":"敏感数据等级",
    "flow":"流量（字节）", # 暂时没有
    "duration":"时长（毫秒）",
    "checkPoint":"检控点id",
    "ccUuid":"全局唯一标识cc-uuid",
}


table_name='audit_check_item'



fields=["operateType","policeNo","subject","object","businessIp","terminalIp","protocolType","srcIp","destIp","srcPort","userToken","appToken","dataLevel","flow","duration","checkPoint","ccUuid"]
fieldInfos=[{"id": 212, "relatedId": 66, "relatedType": "01", "logType": "", "field": "logTime", "validateType": "01", "need": "1", "dataRule": "", "ruleReverse": "0", "expression": "", "describe": "日志业务时间完整性检查项", "advise": "缺失日志业务时间完整性", "block": "0", "score": 1, "createTime": "2023-07-27T15:44:29", "createUser": None, "updateTime": "2023-08-01T10:07:56", "updateUser": None}, {"id": 213, "relatedId": 66, "relatedType": "01", "logType": "", "field": "operateType", "validateType": "03", "need": "0", "dataRule": "1401", "ruleReverse": "0", "expression": "code@audit_dict@type='operate_type'", "describe": "检查报送日志操作类型是否缺失", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-27T16:12:42", "createUser": None, "updateTime": "2023-08-01T10:07:56", "updateUser": None}, {"id": 214, "relatedId": 67, "relatedType": "01", "logType": "", "field": "businessIp", "validateType": "02", "need": "0", "dataRule": "04", "ruleReverse": "0", "expression": "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)$|(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))", "describe": "业务系统IP地址合法性验证", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-27T17:36:35", "createUser": None, "updateTime": "2023-07-31T00:33:29", "updateUser": None}, {"id": 215, "relatedId": 67, "relatedType": "01", "logType": "", "field": "terminalIp", "validateType": "02", "need": "0", "dataRule": "04", "ruleReverse": "0", "expression": "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)$|(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))", "describe": "终端IP地址合法性验证", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-27T17:36:35", "createUser": None, "updateTime": "2023-07-31T00:33:29", "updateUser": None}, {"id": 216, "relatedId": 67, "relatedType": "01", "logType": "", "field": "subject", "validateType": "02", "need": "0", "dataRule": "17", "ruleReverse": "0", "expression": "^[\\w.-]+$", "describe": "验证主体应该是一个简单编码字符串", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-27T17:36:35", "createUser": None, "updateTime": "2023-07-31T00:33:29", "updateUser": None}, {"id": 218, "relatedId": 67, "relatedType": "01", "logType": "", "field": "serviceId", "validateType": "02", "need": "0", "dataRule": "17", "ruleReverse": "0", "expression": "^[\\w.-]+$", "describe": "验证服务ID是一个简单编码字符串", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-27T17:52:59", "createUser": None, "updateTime": "2023-07-31T00:33:29", "updateUser": None}, {"id": 219, "relatedId": 67, "relatedType": "01", "logType": "", "field": "checkSum", "validateType": "02", "need": "0", "dataRule": "17", "ruleReverse": "0", "expression": "^[\\w.-]+$", "describe": "验证签名是一个简单编码字符串", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-27T17:52:59", "createUser": None, "updateTime": "2023-07-31T00:33:29", "updateUser": None}, {"id": 220, "relatedId": 67, "relatedType": "01", "logType": "", "field": "checkPoint", "validateType": "02", "need": "0", "dataRule": "17", "ruleReverse": "0", "expression": "^[\\w.-]+$", "describe": "验证检控点是一个简单编码字符串", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-27T17:52:59", "createUser": None, "updateTime": "2023-07-31T00:33:29", "updateUser": None}, {"id": 221, "relatedId": 67, "relatedType": "01", "logType": "", "field": "ccUuid", "validateType": "02", "need": "0", "dataRule": "17", "ruleReverse": "0", "expression": "^[\\w.-]+$", "describe": "业务唯一标识是一个简单编码字符串", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-27T17:52:59", "createUser": None, "updateTime": "2023-07-31T00:33:29", "updateUser": None}, {"id": 222, "relatedId": 68, "relatedType": "01", "logType": "", "field": "operateType", "validateType": "03", "need": "0", "dataRule": "1401", "ruleReverse": "0", "expression": "code@audit_dict@type='operate_type'", "describe": "业务操作类型验证", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-27T21:36:32", "createUser": None, "updateTime": "2023-07-28T17:41:49", "updateUser": None}, {"id": 223, "relatedId": 68, "relatedType": "01", "logType": "", "field": "dataLevel", "validateType": "03", "need": "0", "dataRule": "1402", "ruleReverse": "0", "expression": "[\"00\",\"01\",\"02\",\"03\"]", "describe": "敏感数据等级验证", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-27T21:36:32", "createUser": None, "updateTime": "2023-07-28T17:41:49", "updateUser": None}, {"id": 224, "relatedId": 69, "relatedType": "01", "logType": "0304", "field": "operateType", "validateType": "03", "need": "0", "dataRule": "1403", "ruleReverse": "1", "expression": "[\"030402\"]", "describe": "权限管理日志-风险日志-频繁命中红/白名单检查", "advise": "", "block": "0", "score": 2, "createTime": "2023-07-27T22:21:22", "createUser": None, "updateTime": "2023-08-30T15:30:15", "updateUser": None}, {"id": 225, "relatedId": 47, "relatedType": "02", "logType": "06", "field": "terminal", "validateType": "02", "need": "0", "dataRule": "04", "ruleReverse": "1", "expression": "83\\.0\\.12\\.49", "describe": "验证总线后台访问客户端地址不在指定范围", "advise": "发现非授权地域访问，需尽快排查防火墙", "block": "0", "score": 3, "createTime": "2023-07-27T22:39:07", "createUser": None, "updateTime": "2023-08-03T13:24:03", "updateUser": None}, {"id": 226, "relatedId": 71, "relatedType": "01", "logType": "", "field": "flags", "validateType": "03", "need": "0", "dataRule": "1404", "ruleReverse": "1", "expression": "[2, 4, 6, 10, 12, 14, 18, 20, 22, 26, 28, 30, 34, 36, 38, 42, 44, 46, 50, 52, 54, 58, 60, 62]", "describe": "", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-28T12:56:01", "createUser": None, "updateTime": "2023-08-09T13:48:37", "updateUser": None}, {"id": 227, "relatedId": 48, "relatedType": "02", "logType": "09", "field": "dataLevel", "validateType": "03", "need": "0", "dataRule": "1402", "ruleReverse": "1", "expression": "[\"01\",\"02\",\"03\"]", "describe": "命中敏感数据", "advise": "发现敏感数据，及时排查", "block": "0", "score": 3, "createTime": "2023-07-28T16:50:46", "createUser": None, "updateTime": "2023-08-01T08:54:38", "updateUser": None}, {"id": 228, "relatedId": 67, "relatedType": "01", "logType": "01", "field": "idcard", "validateType": "02", "need": "0", "dataRule": "06", "ruleReverse": "0", "expression": "(^\\d{8}(0\\d|10|11|12)([0-2]\\d|30|31)\\d{3}$)|(^\\d{6}(18|19|20)\\d{2}(0\\d|10|11|12)([0-2]\\d|30|31)\\d{3}(\\d|X|x)$)", "describe": "", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-28T17:34:56", "createUser": None, "updateTime": "2023-07-31T00:33:29", "updateUser": None}, {"id": 229, "relatedId": 67, "relatedType": "01", "logType": "01", "field": "mobile", "validateType": "02", "need": "0", "dataRule": "07", "ruleReverse": "0", "expression": "^(?:0|86|\\+86)?1[3-9]\\d{9}$", "describe": "", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-28T17:34:56", "createUser": None, "updateTime": "2023-07-31T00:33:29", "updateUser": None}, {"id": 230, "relatedId": 68, "relatedType": "01", "logType": "01", "field": "policeType", "validateType": "03", "need": "0", "dataRule": "1405", "ruleReverse": "0", "expression": "[\"刑警\",\"交警\", \"民警\"]", "describe": "", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-28T17:41:49", "createUser": None, "updateTime": None, "updateUser": None}, {"id": 231, "relatedId": 66, "relatedType": "01", "logType": "01", "field": "username", "validateType": "01", "need": "1", "dataRule": "", "ruleReverse": "0", "expression": "", "describe": "认证日志用户名必须", "advise": "", "block": "0", "score": 1, "createTime": "2023-07-28T18:02:29", "createUser": None, "updateTime": "2023-08-01T10:07:56", "updateUser": None}, {"id": 233, "relatedId": 49, "relatedType": "02", "logType": "01", "field": "terminal", "validateType": "02", "need": "0", "dataRule": "0401", "ruleReverse": "0", "expression": "^(192\\.168|10\\.\\d{1,3}|172\\.(1[6-9]|2[0-9]|3[01]))(\\.\\d{1,3}){2}$", "describe": "管理员以非内网IP登录总线后台", "advise": "管理员以非内网IP登录总线后台，核查系统防火墙及认证策略配置", "block": "0", "score": 1, "createTime": "2023-07-28T20:00:00", "createUser": None, "updateTime": "2023-07-31T00:54:27", "updateUser": None}, {"id": 238, "relatedId": 50, "relatedType": "02", "logType": "0404", "field": "operateType", "validateType": "03", "need": "0", "dataRule": "1406", "ruleReverse": "1", "expression": "[\"040400\"]", "describe": "审批内容命中红名单", "advise": "", "block": "0", "score": 2, "createTime": "2023-07-29T19:11:32", "createUser": None, "updateTime": "2023-07-29T19:47:42", "updateUser": None}, {"id": 239, "relatedId": 51, "relatedType": "02", "logType": "0404", "field": "operateType", "validateType": "03", "need": "0", "dataRule": "1407", "ruleReverse": "1", "expression": "[\"040401\"]", "describe": "审批内容命中白名单策略", "advise": "", "block": "0", "score": 2, "createTime": "2023-07-29T19:32:38", "createUser": None, "updateTime": "2023-07-29T19:36:52", "updateUser": None}, {"id": 240, "relatedId": 66, "relatedType": "01", "logType": "01", "field": "duty", "validateType": "01", "need": "1", "dataRule": "", "ruleReverse": "0", "expression": "", "describe": "", "advise": "", "block": "0", "score": 1, "createTime": "2023-08-01T10:07:56", "createUser": None, "updateTime": None, "updateUser": None}, {"id": 242, "relatedId": 52, "relatedType": "02", "logType": "06", "field": "operateType", "validateType": "03", "need": "0", "dataRule": "1408", "ruleReverse": "1", "expression": "[\"060009\",\"060013\",\"060014\", \"060015\", \"060018\"]", "describe": "", "advise": "", "block": "0", "score": 1, "createTime": "2023-08-03T13:13:57", "createUser": None, "updateTime": "2023-08-03T14:59:54", "updateUser": None}, {"id": 243, "relatedId": 53, "relatedType": "02", "logType": "0301", "field": "object", "validateType": "01", "need": "1", "dataRule": "0401", "ruleReverse": "0", "expression": "^(192\\.168|10\\.\\d{1,3}|172\\.(1[6-9]|2[0-9]|3[01]))(\\.\\d{1,3}){2}$", "describe": "", "advise": "", "block": "0", "score": 0, "createTime": "2023-08-31T10:55:44", "createUser": None, "updateTime": "2023-09-05T14:14:54", "updateUser": None}, {"id": 244, "relatedId": 79, "relatedType": "01", "logType": "", "field": "subject", "validateType": "02", "need": "0", "dataRule": "08", "ruleReverse": "0", "expression": "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])", "describe": "", "advise": "", "block": "0", "score": 0, "createTime": "2023-08-31T18:24:12", "createUser": None, "updateTime": None, "updateUser": None}]
fieldInfos_dict=base_client.convert_to_dict_set(fieldInfos,key='field',over_write=False)


def join_field_param(field_req_param, field_info):
    field_req_param['fieldName'] = f'{field_info["field"]}'
    field_req_param['fieldZh'] = f'{field_desc_dict[field_info["field"]]}'
    field_req_param['fieldDesc'] = f'{field_desc_dict[field_info["field"]]}'
    field_req_param['mustAdvise'] = f'{field_req_param["fieldDesc"]}完整性检查项'
    field_req_param['ruleAdvise'] = f'{field_req_param["fieldDesc"]}规则性检查项'
    field_req_param['expression'] = f'{field_info["expression"]}'
    field_req_param['dataRule'] = f'{field_info["dataRule"]}'

    pageOptJson = dict()
    pageOptJson.update(field_req_param['pageOptJson'])
    pageOptJson['field']=f'{field_info["field"]}'
    pageOptJson['label']=f'{field_info["field"]}'
    field_req_param['pageOptJson']=pageOptJson

    return field_req_param


def join_param(logType, desc):
    req_param=dict()
    req_param.update(base_param)
    req_param['desc'] = f'{desc}基础策略'
    req_param['name'] = f'{desc}基础策略'
    req_param['logType'] = f'{logType}'

    fieldInfos=[]
    for field in fields:
        if  fieldInfos_dict.get(field)!=None:
            field_req_param=dict()
            field_req_param.update(field_base_param)
            field_req_param=join_field_param(field_req_param,fieldInfos_dict.get(field))
            fieldInfos.append(field_req_param)
    req_param['fieldInfos']=fieldInfos
    return req_param



for k,v in log_types.items():
    req_param=join_param(k,v)
    headers={
        "Cookie":"DSJRZ-USERTOKEN=jg1.aedb022975ff4103850727d5570a3b18; RZZX-USERTOKEN=jg1.aedb022975ff4103850727d5570a3b18; DSJRZ-APPTOKEN=jg1.ed482c2615214616a3d3385adfbc7866; RZZX-APPTOKEN=jg1.ed482c2615214616a3d3385adfbc7866",
        "RZZX-APPTOKEN":"jg1.ed482c2615214616a3d3385adfbc7866",
        "DSJRZ-APPTOKEN":"jg1.ed482c2615214616a3d3385adfbc7866",
        "RZZX-USERTOKEN":"jg1.aedb022975ff4103850727d5570a3b18",
        "DSJRZ-USERTOKEN":"jg1.aedb022975ff4103850727d5570a3b18",

             }
    res=http_client.post(url=req_url,data=req_param,headers=headers,fast_falg=True)
    print(res)


